/*
 * @Descripttion:
 * @Author: liuyu
 * @Date: 2023-12-04 15:44:23
 */
'use strict';

const Controller = require('egg').Controller;
class AuthController extends Controller {
  // 登录
  async login() {
    const { ctx } = this;
    const { service, helper, request } = ctx;
    const { username, password } = request.body;
    const userInfo = await service.user.show({ username });
    console.log(username, password);
    if (!userInfo) {
      helper.render(422, {}, '用户未注册');
      return;
    }
    const md5password = helper.addSaltPassword(password);
    console.log(md5password);
    if (userInfo.password !== md5password) {
      helper.render(422, {}, '密码错误');
      return;
    }
    const adminUserInfo = await service.userAdmin.show({ un: username });
    // 设置token
    const token = await helper.loginToken({
      userAdminId: adminUserInfo.id,
      userAdminBid: adminUserInfo.bid,
      id: userInfo.id,
      username: userInfo.username,
      role_id: userInfo.role_id,
    }, 1000 * 60 * 24 * 1);
    // ctx.set('authorization', token);
    helper.render(200, {
      token,
    }, '登录成功');
  }

  async register() {
    const { ctx } = this;
    const { service, helper, request } = ctx;
    const body = request.body;
    try {
      await service.userAdmin.createBusiness(body);
      helper.render(200, {}, '添加成功');
    } catch (e) {
      helper.render(422, e, '添加失败');
    }
  }

  // 登录
  async info() {
    const { ctx } = this;
    const { helper } = ctx;
    console.log(ctx.locals.auth);
    const userInfo = await ctx.service.userAdmin.show({ id: ctx.locals.auth.userAdminId });
    const businessInfo = await ctx.service.userAdmin.business({ id: userInfo.bid });
    const role = await ctx.service.role.show({
      role_id: ctx.locals.auth.role_id,
    });
    // console.log(role);
    helper.render(200, {
      data: {
        name: businessInfo.name,
        isadmin: userInfo.isadmin,
        username: userInfo.un,
        role_id: ctx.locals.auth.role_id,
        role: role.role_name,
        desc: businessInfo.desc,
        logo: businessInfo.logo,
        linkman: businessInfo.linkman,
        linktel: businessInfo.linktel,
        money: businessInfo.money,
      },
    }, '登录成功');
  }

  // 获取角色菜单
  async menu() {
    const { ctx } = this;
    const role_menu_ids = await ctx.service.role.menuids(ctx.locals.auth.role_id);
    console.log(role_menu_ids);
    let menus = [];
    console.log(ctx.locals.auth);
    // eslint-disable-next-line eqeqeq
    if (ctx.locals.auth.role_id == 0) {
      menus = await ctx.service.menu.index({});
    } else {
      menus = await ctx.service.menu.index({
        menu_id: role_menu_ids,
      });
      console.log(menus);
    }
    // 递归查找父节点（栏目）
    const allMenu = ctx.helper.toTree(menus, 'parent_id', 'menu_id');
    ctx.helper.render(200, {
      data: allMenu,
    }, '获取成功');
  }

  async show() {
    const { ctx: { service, helper, params } } = this;
    const res = await service.userAdmin.showAdminUser({ id: params.id });
    helper.render(200, {
      data: res,
    });
  }
  async list() {
    const { ctx } = this;
    const { page = 1, page_size = 10, field, order, ...where } = ctx.query;

    if (where.name) {
      where.name = `%${where.name}%`;
    }
    console.log(where);
    const res = await ctx.service.userAdmin.selectAdminUser([ 'id', 'username', 'role_id', 'name', 'phone' ], {
      ...where,
      // id: [{ op: '!=', value: ctx.locals.auth.id }],
      bid: ctx.locals.auth.userAdminBid,
    }, page, page_size, field, order);

    const total = await this.app.mysql.get('db1').count('user', {
      ...where,
      // id: [{ op: '<>', value: ctx.locals.auth.id }],
      bid: ctx.locals.auth.userAdminBid,
    });

    ctx.helper.render(200, {
      data: res,
      total,
    });
  }
  async create() {
    const { ctx: { service, helper, request, locals } } = this;
    const body = request.body;
    const showRes = await service.userAdmin.showAdminUser({ username: body.username, bid: locals.auth.userAdminBid });
    if (showRes) {
      helper.render(422, {}, '员工账号已存在');
      return;
    }
    try {
      await service.userAdmin.createAdminUser({
        ...body,
        bid: locals.auth.userAdminBid,
      });
      helper.render(200, {}, '添加成功');
    } catch (e) {
      helper.render(422, e, '添加失败');
    }
  }
  async update() {
    const { ctx: { service, helper, params, request } } = this;
    const body = request.body;
    const where = { id: params.id * 1 };
    delete body.id;
    try {
      await service.userAdmin.updateAdminUser(body, where);
      helper.render(200, {}, '更新成功');
    } catch (e) {
      helper.render(422, e, '更新失败');
    }
  }
  async delete() {
    const { ctx: { service, helper, params, request } } = this;
    try {
      await service.userAdmin.deleteAdminUser(params.id || request.body.ids);
      helper.render(200, {}, '删除成功');
    } catch (e) {
      helper.render(422, e, '删除失败');
    }
  }
}

module.exports = AuthController;
